

//
//  CommentShowVc.m
//  WGFGlobalHouses
//
//  Created by Andy on 15/7/17.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CommentShowVc.h"
#import "HourseDataManage.h"
#import "MJRefresh.h"
#import "CommentCellP.h"
#import "BdTV.h"



@interface CommentShowVc ()
@property (nonatomic,strong)NSArray *dataArray;
@property (strong, nonatomic) HourseDataManage *dm;
@property(nonatomic,assign) int pageIndex;
@property (strong, nonatomic) NSString *hourseId;

@end

@implementation CommentShowVc
@synthesize dm,hourseId,pageIndex,dataArray,nodataCell;



-(instancetype)initWithHourseId:(NSString*)data{
    self = [super init];
    if (self) {
        self.hourseId = data;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    
    [self refreshData];
}




-(void)initView{
    
    pageIndex = 1;
    
    dm = [[HourseDataManage alloc]init];
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];

    
    BdTV *title = [BdTV creatInstanceWithTitle:@"用户点评"];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] setTitle:@"添加" forState:UIControlStateNormal];
    [[title doneBtn]setTag:12];
    [[title doneBtn]setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [title setRightImg:nil];
    [[title doneBtn]addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                                             initWithCustomView:title];
    
}


-(void)btnOnClick:(UIButton *)sender{
    
    if (sender.tag==11) {
        
        [self.navigationController popViewControllerAnimated:YES];

        
    }else if(sender.tag==12){
        
    
    }else if (sender.tag==56){
    
    
        
    }
}



-(void)refreshData{
    
    [self.tableView.footer resetNoMoreData];
    [self.nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod =[dm refreshAllCommentData:self.hourseId pageIndex:pageIndex];
        
        bself.dataArray = mod.dataMod;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                if(bself.dataArray.count==0){//没数据
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                    
                }else{//有数据
                    
                    pageIndex++;
                    [bself.nodataCell setState:nodataCellStateNorMal];
                }
            }
            
            [bself.tableView reloadData];
            
            [bself.tableView.header endRefreshing];
        });
    });
}


- (void)loadMore{
    [super loadMore];
    
    [self.tableView.footer resetNoMoreData];
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dm refreshAllCommentData:self.hourseId pageIndex:pageIndex];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [bself.tableView.footer endRefreshing];
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                
            }else if (mod.resltState == ResultFail){//服务器返回的code出错
                
                
            }else{
                
                NSMutableArray *array = [[NSMutableArray alloc]initWithArray:mod.dataMod];
                if (array.count == 0 ||array == nil) {
                    [bself.tableView.footer noticeNoMoreData];
                }else{
                    pageIndex ++;
                    [array addObjectsFromArray:bself.dataArray];
                    bself.dataArray = [[NSArray alloc]initWithArray:array];
                }
            }
            
            [bself.tableView reloadData];
        });
    });
}





#pragma tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.dataArray.count;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count==0) {
        if(!nodataCell){
            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
            [nodataCell setState:nodataCellStateLoading];
            [[nodataCell refreshBtn]setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }
    
    
    
    CommentCellP *cell = [CommentCellP cellWithTableView:tableView];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (dataArray&&dataArray.count>0) {
        
        return  [CommentCellP getHight:nil withTableView:tableView];
    }
    
    return self.tableView.frame.size.height;
    
}




@end
